package carmesi.wizards;

import java.awt.Component;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;
import org.netbeans.spi.project.ui.templates.support.Templates;
import org.openide.WizardDescriptor;
import org.openide.WizardDescriptor.Panel;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;

/**
 *
 * @author Victor
 */
public abstract class CarmesiWizardIterator implements WizardDescriptor.InstantiatingIterator{
    private Panel panels[];
    private int currentStep;
    private WizardDescriptor wizardDescriptor;

    @Override
    public Set instantiate() throws IOException {
        //Get the template and convert it:
        FileObject template = Templates.getTemplate(wizardDescriptor);
        DataObject dTemplate = DataObject.find(template);

        //Get the package:
        FileObject dir = Templates.getTargetFolder(wizardDescriptor);
        DataFolder df = DataFolder.findFolder(dir);

        //Get the class:
        String targetName = Templates.getTargetName(wizardDescriptor);
        Map<String, String> args=new HashMap<String, String>();
        setTemplateArguments(args);
        
        //Define the template from the above,
        //passing the package, the file name, and the map of strings to the template:
        DataObject dobj = dTemplate.createFromTemplate(df, targetName, args);

        //Obtain a FileObject:
        FileObject createdFile = dobj.getPrimaryFile();

        //Create the new file:
        return Collections.singleton(createdFile);
    }

    public WizardDescriptor getWizardDescriptor() {
        return wizardDescriptor;
    }
    
    @Override
    public void initialize(WizardDescriptor wd) {
        wizardDescriptor=wd;
        panels=createPanels();
        currentStep=0;
        String stepNames[]=new String[panels.length];
        for(int i=0; i < panels.length; i++){
            stepNames[i]=panels[i].getComponent().getName();
        }
        for (int i = 0; i < panels.length; i++) {
            Component c = panels[i].getComponent();
//            if (steps[i] == null) {
//                // Default step name to component name of panel. Mainly
//                // useful for getting the name of the target chooser to
//                // appear in the list of steps.
//                steps[i] = c.getName();
//            }
            if (c instanceof JComponent) { // assume Swing components
                JComponent jc = (JComponent) c;
                // Sets step number of a component
                // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_*:
                jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
                // Sets steps names for a panel
                jc.putClientProperty("WizardPanel_contentData", stepNames);
                // Turn on subtitle creation on each step
                jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
                // Show steps on the left side with the image on the background
                jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
                // Turn on numbering of all steps
                jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
            }
        }
    }

    @Override
    public void uninitialize(WizardDescriptor wd) {
        
    }

    @Override
    public Panel current() {
        return panels[currentStep];
    }

    @Override
    public String name() {
        return panels[currentStep].getComponent().getName();
    }

    @Override
    public boolean hasNext() {
        return currentStep < panels.length- 1;
    }

    @Override
    public boolean hasPrevious() {
        return currentStep > 0;
    }

    @Override
    public void nextPanel() {
        currentStep++;
    }

    @Override
    public void previousPanel() {
        currentStep--;
    }

    @Override
    public void addChangeListener(ChangeListener cl) {
        
    }

    @Override
    public void removeChangeListener(ChangeListener cl) {
        
    }

    protected abstract Panel[] createPanels();

    //Template method to be overriden by subclasses
    protected void setTemplateArguments(Map<String, String> args){
        //do nothing
    }
    
    public CarmesiPanel createPanel(final Component component){
        return new CarmesiPanel(component);
    }
    
}
